home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / SYSVIEW / SYSVIEW.C < prev   
Encoding:
C/C++ Source or Header  |  1998-08-12  |  8.7 KB  |  443 lines

  1.  
  2. /*
  3.  * Systems statistics viewing application
  4.  *  (C) Javier Velasco    '97 (almost '98)
  5.  *    fjvelasco@sinix.net
  6.  *
  7.  *  This application was developed on an INDIGO2 Extreme and makes use of
  8.  *  a SGI system call (sginap) that sleeps the process for a given number of
  9.  *  clock ticks. For other UNIX systems, this call should be substituted for 
  10.  *  another proper call.
  11.  *  The default number of ticks between samples is 20. This can be changed
  12.  *  through the mouse right button menu.
  13.  *
  14.  */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <sys/sysmp.h>
  20. #include <sys/sysinfo.h>
  21. #include <sys/time.h>
  22. #include <unistd.h>
  23.  
  24. /* GL includes */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <X11/Xlib.h>
  30. #include <X11/Xutil.h>
  31.  
  32.  
  33. /*
  34.  * Macros
  35.  */
  36. #define GRID        0x22
  37. #define ZGRID        0x23
  38. #define XGRID        0x24
  39. #define YGRID        0x25
  40.  
  41. float lastx=0;    /* mouse track */
  42. float lasty=0;
  43.  
  44. void *font1 = GLUT_BITMAP_9_BY_15; /* used fonts */
  45. void *font2 = GLUT_BITMAP_8_BY_13;
  46.  
  47. long nPeriod=20;        /* default sampling rate in ticks */
  48. GLsizei nWidth, nHeight;    /* current window size */
  49. GLboolean bMotion=False;
  50.  
  51. struct sysinfo SysInfo, LastSysInfo;    /* system information */
  52.  
  53. /*
  54.  * Mouse motion track
  55.  */
  56. void MouseMove(int x, int y)
  57. {
  58.     if(bMotion)
  59.     {
  60.     lastx = x;
  61.     lasty = y;
  62.     glutPostRedisplay();    
  63.     }
  64. }
  65.  
  66. /*
  67.  * 3D Perspective projection setup
  68.  */
  69. void Make3DLook(void)
  70. {
  71.     glMatrixMode(GL_PROJECTION);
  72.     glLoadIdentity();
  73.     gluPerspective(45.0, 4.0/3.0, 0.0, 500.0);
  74.     glMatrixMode(GL_MODELVIEW);
  75.     glLoadIdentity();    
  76. }
  77.  
  78. /*
  79.  * 2D Orthographic projections setup
  80.  */
  81. void Make2DLook(void)
  82. {
  83.   glMatrixMode(GL_PROJECTION);
  84.   glLoadIdentity();
  85.   gluOrtho2D(0, nWidth, nHeight, 0);
  86.   glMatrixMode(GL_MODELVIEW);
  87. }
  88.  
  89. /*
  90.  * Toggle line antialias 
  91.  */
  92. void ToggleAAlias(void)
  93. {
  94.     if(glIsEnabled(GL_LINE_SMOOTH))
  95.     {
  96.     glDisable(GL_LINE_SMOOTH);        
  97.     glDisable(GL_BLEND);        
  98.     }
  99.     else
  100.     {
  101.     glEnable(GL_LINE_SMOOTH);
  102.     glEnable(GL_BLEND);
  103.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  104.         glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  105.     }
  106. }
  107.  
  108.  
  109. /*
  110.  * Draw a string
  111.  */
  112. void glPuts(GLint x, GLint y, char *string, void *font)
  113. {
  114.   int len, i;
  115.  
  116.   glRasterPos2i(x, y);
  117.   len = (int) strlen(string);
  118.   for (i = 0; i < len; i++) {
  119.     glutBitmapCharacter(font, string[i]);
  120.   }
  121. }
  122.  
  123. /*
  124.  * Draw the 3d grid
  125.  */
  126. void DrawGrid(void)
  127. {
  128.     glCallList(GRID);
  129. }
  130.  
  131. /*
  132.  * Display the legend
  133.  */
  134. void Legend(void)
  135. {
  136.     glPuts(5, 40, "User", font2);    
  137.     glPushMatrix();
  138.     glPushAttrib(GL_CURRENT_BIT);
  139.     glTranslatef(10.0, 50.0, 0.0);
  140.     glColor3f(0.0, 0.0, 1.0);
  141.     glRecti(0, 0, 20, 20);
  142.     glPopAttrib();
  143.     glPopMatrix();
  144.     
  145.     glPuts(5, 90, "Kernel", font2);    
  146.     glPushMatrix();
  147.     glPushAttrib(GL_CURRENT_BIT);
  148.     glTranslatef(10.0, 100.0, 0.0);
  149.     glColor3f(1.0, 0.0, 0.0);
  150.     glRecti(0, 0, 20, 20);
  151.     glPopAttrib();
  152.     glPopMatrix();
  153.  
  154.     glPuts(5, 140, "Intr", font2);    
  155.     glPushMatrix();
  156.     glPushAttrib(GL_CURRENT_BIT);
  157.     glTranslatef(10.0, 150.0, 0.0);
  158.     glColor3f(1.0, 1.0, 0.0);
  159.     glRecti(0, 0, 20, 20);
  160.     glPopAttrib();
  161.     glPopMatrix();
  162.  
  163.     glPuts(5, 190, "Idle", font2);    
  164.     glPushMatrix();
  165.     glPushAttrib(GL_CURRENT_BIT);
  166.     glTranslatef(10.0, 200.0, 0.0);
  167.     glColor3f(0.0, 1.0, 0.0);
  168.     glRecti(0, 0, 20, 20);
  169.     glPopAttrib();
  170.     glPopMatrix();
  171.     
  172.     glPuts(5, 240, "Wait", font2);    
  173.     glPushMatrix();
  174.     glPushAttrib(GL_CURRENT_BIT);
  175.     glTranslatef(10.0, 250.0, 0.0);
  176.     glColor3f(0.0, 1.0, 1.0);
  177.     glRecti(0, 0, 20, 20);
  178.     glPopAttrib();
  179.     glPopMatrix();
  180.     
  181.     glPuts(nWidth-30, nHeight-30, "0", font1); 
  182.     glPuts(nWidth-45, 45, "100", font1); 
  183. }
  184.  
  185.  
  186.  
  187. /*
  188.  * Construct the grid display list
  189.  */
  190. void MakeGrid(void)
  191. {
  192.     int i;
  193.     
  194.     /* let's divide the grid in three pieces */
  195.     glNewList(ZGRID, GL_COMPILE_AND_EXECUTE);
  196.     glBegin(GL_LINES);
  197.     for(i=0;i<=30;i+=3)
  198.     {
  199.     glVertex2d(i, 0);
  200.     glVertex2d(i, 30);
  201.     glVertex2d(0, i);
  202.     glVertex2d(30, i);
  203.     }
  204.     glEnd();
  205.     glEndList();
  206.     
  207.         
  208.     glNewList(XGRID, GL_COMPILE_AND_EXECUTE);
  209.     glPushMatrix();
  210.     glRotatef(90.0, 1.0, 0.0, 0.0);    
  211.     glBegin(GL_LINES);
  212.     for(i=0;i<=6;i+=3)
  213.     {
  214.     glVertex2d(0, i);
  215.     glVertex2d(30, i);
  216.     }
  217.     for(i=0;i<=30;i+=3)
  218.     {
  219.     glVertex2d(i, 0);
  220.     glVertex2d(i, 6);
  221.     } 
  222.     glEnd();
  223.     glPopMatrix();
  224.     glEndList();
  225.  
  226.     glNewList(YGRID, GL_COMPILE_AND_EXECUTE);
  227.     glPushMatrix();
  228.     glRotatef(-90.0, 0.0, 1.0, 0.0);    
  229.     glBegin(GL_LINES);
  230.     for(i=0;i<=30;i+=3)
  231.     {
  232.     glVertex2d(0, i);
  233.     glVertex2d(6, i);
  234.     }
  235.     for(i=0;i<=6;i+=3)
  236.     {
  237.     glVertex2d(i, 0);
  238.     glVertex2d(i, 30);
  239.     } 
  240.     glEnd();
  241.     glPopMatrix();
  242.     glEndList();
  243.  
  244.     /* now call them all */
  245.     glNewList(GRID, GL_COMPILE_AND_EXECUTE);
  246.     glLineWidth(2.0);
  247.     glColor3f(0.7, 0.7, 0.7);
  248.     glCallList(ZGRID);
  249.     glCallList(XGRID);
  250.     glCallList(YGRID);
  251.     glLineWidth(1.0);
  252.     glEndList();
  253. }
  254.  
  255. /*
  256.  * System statistics collection routine
  257.  */
  258. void ComputeStatistics(void)
  259. {
  260.     memcpy(&LastSysInfo, &SysInfo, sizeof(SysInfo));
  261.     sysmp(MP_SAGET, MPSA_SINFO, &SysInfo, sizeof(SysInfo));
  262.     sginap(nPeriod);
  263.     glutPostRedisplay();
  264. }
  265.  
  266. /*
  267.  * Draw a coloured cube for each one of the monitored parameters
  268.  */
  269. void Draw3DStatistics(void)
  270. {
  271.     GLdouble fSize;
  272.  
  273.     glPushMatrix();
  274.     glTranslatef(0.0, 0.0, 1.5);
  275.     /* Time in user mode */
  276.     glPushMatrix();
  277.     fSize = SysInfo.cpu[CPU_USER] - LastSysInfo.cpu[CPU_USER] ;
  278.     fSize = fSize*30/nPeriod;
  279.     glTranslatef(3.0, fSize/2, 0.0);
  280.     glScalef(6.0, fSize, 3.0);
  281.     glColor3f(0.0, 0.0, 1.0);
  282.     glutSolidCube(1.0);
  283.     glColor3f(0.0, 0.0, 0.0);
  284.     glutWireCube(1.0);
  285.     glPopMatrix();
  286.     
  287.     /* Time in kernel mode */
  288.     glPushMatrix();
  289.     fSize = SysInfo.cpu[CPU_KERNEL] - LastSysInfo.cpu[CPU_KERNEL] ;
  290.     fSize = fSize*30/nPeriod;
  291.     glTranslatef(9.0, fSize/2, 0.0);
  292.     glScalef(6.0, fSize, 3.0);
  293.     glColor3f(1.0, 0.0, 0.0);
  294.     glutSolidCube(1.0);
  295.     glColor3f(0.0, 0.0, 0.0);
  296.     glutWireCube(1.0);
  297.     glPopMatrix();
  298.  
  299.    /* Time in interrupt mode */
  300.     glPushMatrix();
  301.     fSize = SysInfo.cpu[CPU_INTR] - LastSysInfo.cpu[CPU_INTR] ;
  302.     fSize = fSize*30/nPeriod;
  303.     glTranslatef(15.0, fSize/2, 0.0);
  304.     glScalef(6.0, fSize, 3.0);
  305.     glColor3f(1.0, 1.0, 0.0);
  306.     glutSolidCube(1.0);
  307.     glColor3f(0.0, 0.0, 0.0);
  308.     glutWireCube(1.0);
  309.     glPopMatrix();
  310.   
  311.    /* Time in idle */
  312.     glPushMatrix();
  313.     fSize = SysInfo.cpu[CPU_IDLE] - LastSysInfo.cpu[CPU_IDLE] ;
  314.     fSize = fSize*30/nPeriod;
  315.     glTranslatef(21.0, fSize/2, 0.0);
  316.     glScalef(6.0, fSize, 3.0);
  317.     glColor3f(0.0, 1.0, 0.0);
  318.     glutSolidCube(1.0);
  319.     glColor3f(0.0, 0.0, 0.0);
  320.     glutWireCube(1.0);
  321.     glPopMatrix();
  322.   
  323.    /* Time in wait mode */
  324.     glPushMatrix();
  325.     fSize = SysInfo.cpu[CPU_WAIT] - LastSysInfo.cpu[CPU_WAIT] ;
  326.     fSize = fSize*30/nPeriod;
  327.     glTranslatef(27.0, fSize/2, 0.0);
  328.     glScalef(6.0, fSize, 3.0);
  329.     glColor3f(0.0, 1.0, 1.0);
  330.     glutSolidCube(1.0);
  331.     glColor3f(0.0, 0.0, 0.0);
  332.     glutWireCube(1.0);
  333.     glPopMatrix();
  334.         
  335.     glPopMatrix();
  336. }
  337.  
  338.  
  339. /*
  340.  * Resize routine
  341.  */
  342. void Resize3DWindow(int newWidth, int newHeight)
  343. {
  344.     glViewport(0, 0, (GLint)newWidth, (GLint)newHeight);
  345.     nWidth = newWidth;
  346.     nHeight = newHeight;
  347.     Make3DLook();
  348.     glClear(GL_COLOR_BUFFER_BIT);
  349. }
  350.  
  351.  
  352. /*
  353.  * Display routine
  354.  */
  355. void Doit3D(void)
  356. {
  357.  
  358.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  359.     Make2DLook();
  360.     glColor3f(1.0, 1.0, 0.0);
  361.     glPuts(15, 15, "CPU Activity", font1);
  362.     Legend();
  363.     Make3DLook();
  364.     glPushMatrix();
  365.     glTranslatef(-15.0, -15.0, -50.0);
  366.     glRotatef (lastx, 0.0, 1.0, 0.0);
  367.     glRotatef (lasty, 1.0, 0.0, 0.0);
  368.     DrawGrid();
  369.     Draw3DStatistics();
  370.     glFlush();
  371.     glPopMatrix();
  372.     glutSwapBuffers();
  373. }
  374.  
  375. /*
  376.  * Menus routines
  377.  */
  378. void SelectSampleRate(int pick)
  379. {
  380.     nPeriod = pick;
  381. }
  382.  
  383. void MainMenu(int pick)
  384. {
  385.     switch(pick)  
  386.     {
  387.     case 0:
  388.         bMotion = !bMotion;
  389.         break;
  390.     case 1:
  391.         ToggleAAlias();
  392.         break;        
  393.     case 2:
  394.         exit(0);
  395.         break;        
  396.     }
  397. }
  398.  
  399. /*
  400.  * Menu creation
  401.  */
  402. void SetUpMenu(void)
  403. {
  404.     int SampleMenu;
  405.     
  406.     SampleMenu = glutCreateMenu(SelectSampleRate);
  407.     glutAddMenuEntry("1", 1);
  408.     glutAddMenuEntry("5", 5);
  409.     glutAddMenuEntry("10", 10);
  410.     glutAddMenuEntry("20", 20);
  411.     glutCreateMenu(MainMenu);
  412.     glutAddMenuEntry("Mouse Motion", 0);
  413.     glutAddMenuEntry("Line antialias", 1);
  414.     glutAddSubMenu("Sample rate", SampleMenu);
  415.     glutAddMenuEntry("Quit", 2);
  416.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  417.  
  418. }
  419.  
  420. /*
  421.  * MAIN STUFF
  422.  */
  423. int main(int argc,  char **argv)
  424. {
  425.     GLenum type;
  426.     
  427.     type = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH;
  428.     glutInit(&argc, argv);
  429.     glutInitDisplayMode(type);
  430.     glutCreateWindow(argv[0]);    
  431.     SetUpMenu();
  432.     
  433.     MakeGrid();
  434.  
  435.     glutReshapeFunc(Resize3DWindow);
  436.     glutDisplayFunc(Doit3D);
  437.     glutMotionFunc(MouseMove);
  438.     glutIdleFunc(ComputeStatistics);
  439.     glutMainLoop();
  440.     return(0);
  441. }
  442.  
  443.